package vn.edu.hut.soict.alignment;

import java.util.HashMap;
import java.util.List;

import vn.edu.hut.soict.translate.utils.StringUtils;

public class BiDirectionalSyllableAlignment extends BiDirectionalAlignment {

	public BiDirectionalSyllableAlignment(List<String> source, List<String> destination, String alignmentCode) {
		this.source = source;
		this.destination = destination;
		parseAlignment(alignmentCode);
		reverseAlignment();
	}

	public BiDirectionalSyllableAlignment(String source, String destination, String alinmentCode) {
		this(StringUtils.splitIntoSyllable(source), StringUtils.splitIntoSyllable(destination), alinmentCode);
	}

	public BiDirectionalSyllableAlignment(List<String> source, List<String> destination) {
		this.source = source;
		this.destination = destination;
		alignment = new HashMap<Integer, List<Integer>>();
		reversedAlignment = new HashMap<Integer, List<Integer>>();
	}

	public BiDirectionalSyllableAlignment(String source, String destination) {
		this(StringUtils.splitIntoSyllable(source), StringUtils.splitIntoSyllable(destination));
	}

	private void parseAlignment(String alignmentCode) {
		alignment = new HashMap<Integer, List<Integer>>();
		String[] split1 = alignmentCode.split("\\s+");
		for (int i = 0; i < split1.length; i++) {
			String[] split2 = split1[i].split("#");
			Integer sourceSyllableIndex = Integer.parseInt(split2[0]);
			if (sourceSyllableIndex == source.size()) {
				sourceSyllableIndex = null;
			}
			for (int j = 1; j < split2.length; j++) {
				String[] split3 = split2[j].split("\\|");
				Integer destinationSyllableIndex = Integer.parseInt(split3[0]);
				if (destinationSyllableIndex == destination.size()) {
					destinationSyllableIndex = null;
				}
				addToAlignmentMap(alignment, sourceSyllableIndex, destinationSyllableIndex);
			}
		}
	}
}
